****************************************************************************
*This file builds vars on schooling in long-differences from the Census/ACS*
****************************************************************************

use "$list/czones_list.dta", clear

merge 1:1 czone using "$clean_data_lmarket/czone1990_schooling.dta", assert(3) nogenerate
merge 1:1 czone using "$clean_data_lmarket/czone2000_schooling.dta", assert(3) nogenerate
merge 1:1 czone using "$clean_data_lmarket/czone2008_schooling.dta", assert(3) nogenerate
merge 1:1 czone using "$clean_data_lmarket/czone2016_schooling.dta", assert(3) nogenerate

merge 1:1 czone using "$clean_data_lmarket/czone1980_schooling.dta", assert(3) nogenerate


foreach var of varlist ipums_* {
quiet replace `var' = 0 if mi(`var')
}

*Build alternative age groups
foreach year in 1980 1990 2000 2008 2016 {
foreach var in pop pop_sc {
gen ipums_`var'_a19_34_`year' = ipums_`var'_a19_24_`year'+ipums_`var'_a25_29_`year'+ipums_`var'_a30_34_`year'
gen ipums_`var'_a35_49_`year' = ipums_`var'_a35_39_`year'+ipums_`var'_a40_44_`year'+ipums_`var'_a45_49_`year'
gen ipums_`var'_a50_64_`year' = ipums_`var'_a50_54_`year'+ipums_`var'_a55_59_`year'+ipums_`var'_a60_64_`year'
gen ipums_`var'_a35_64_`year' = ipums_`var'_a35_49_`year'+ipums_`var'_a50_64_`year'

}
}

*Schooling
foreach school in sc {

gen d_`school' = 100*((ipums_pop_`school'_2008/ipums_pop_2008)-(ipums_pop_`school'_1990/ipums_pop_1990))

gen share_`school'_1990 = 100*ipums_pop_`school'_1990/ipums_pop_1990

*Pre-trends (written as _1980 instead of _80_90 because needed for table of descriptives)
gen pd_`school'_1980 = 100*((ipums_pop_`school'_1990/ipums_pop_1990)-(ipums_pop_`school'_1980/ipums_pop_1980))

gen pshare_`school'_1980 = 100*ipums_pop_`school'_1980/ipums_pop_1980

*Age
foreach age in a19_24 a25_29 a30_34 a35_49 a50_64 a19_34 a35_64 {

gen d_`school'_`age' = 100*((ipums_pop_`school'_`age'_2008/ipums_pop_`age'_2008)-(ipums_pop_`school'_`age'_1990/ipums_pop_`age'_1990))

gen share_`school'_`age'_1990 = 100*ipums_pop_`school'_`age'_1990/ipums_pop_`age'_1990

*Pre-trends (written as _1980 instead of _80_90 because needed for table of descriptives)
gen pd_`school'_`age'_1980 = 100*((ipums_pop_`school'_`age'_1990/ipums_pop_`age'_1990)-(ipums_pop_`school'_`age'_1980/ipums_pop_`age'_1980))

gen pshare_`school'_`age'_1980 = 100*ipums_pop_`school'_`age'_1990/ipums_pop_`age'_1980

}
}

*Keep variables of interest
keep czone pshare_* share_* d_* pd_* ipums_pop_1990 ipums_pop_1980
drop *_a??_??_1980 pd_*_a??_??_1980

save "$final_data_outcomes/czone_StudentsLongDifferences.dta", replace






